import { Service } from 'egg';

export default class Feedback extends Service {
    /**
    * 获取列表
    * */
    public async getList() {
        const { ctx, app } = this;
        const { Joi } = app;

        let schema = Joi.object({
            pageNum: Joi.number().required(),
            pageSize: Joi.number().max(21).required(),
        });
        let { error } = ctx.validate(schema, ctx.query);
        if (error) {
            ctx.status = 403;
            return { error: error.message };
        };
        let { pageSize, pageNum } = ctx.query
        let list = await app.mysql.get(app.dbName).select('feedback', {
            limit: Number(pageSize),
            offset: Number(pageSize) * (Number(pageNum) - 1),
            orders:[['create_time','desc']]
            // columns:['contact','location','create_time']
        });
        let total = (await app.mysql.get(app.dbName).query('SELECT count(*) AS total FROM feedback'))[0]?.total;
        return {
            code: 200,
            data: {
                list,
                total
            }
        }
    }
    /**
    * 获取客户端列表（用户列表）
    * */
    public async getDeviceList() {
        const { ctx, app } = this;
        const { Joi } = app;

        let schema = Joi.object({
            pageNum: Joi.number().required(),
            pageSize: Joi.number().max(100).required(),
        });
        let { error } = ctx.validate(schema, ctx.query);
        if (error) {
            ctx.status = 403;
            return { error: error.message };
        };
        let { pageSize, pageNum } = ctx.query

        let total = await app.mysql.get(app.dbName).query('SELECT count(*) AS total FROM imei_list');
        // let sql = `SELECT * FROM imei_list ORDER BY id DESC LIMIT ${(Number(pageNum) - 1) * Number(pageSize)},${pageSize}`
        let list = (await app.mysql.get(app.dbName).select('imei_list', {
            limit: parseInt(pageSize),
            offset: parseInt(pageSize) * (parseInt(pageNum) - 1),
            orders:[['create_time','desc']]
            // columns:['contact','location','create_time']
        })) || []
        return {
            code: 200,
            data: {
                list,
                total: (total || [{}])[0].total
            }
        }
    }
}